﻿using AutoMapper;
using SmartConference.Api.Applocations.IServices;
using SmartConference.Api.DTO;
using SmartConference.Domain;
using SmartConference.ErrorCode;
using SmartConference.Infrastructure.Interfaces;
using System.ComponentModel;
using System.Transactions;

namespace SmartConference.Api.Applocations.Services
{
    public class MeetingAppointServices : IMeetingAppointServices
    {
        private readonly IMeetingAppointRepository meetingAppointRepository;
        private readonly IMeetingRoomRepository meetingRoomRepository;
        private readonly IMeetingApprovalRepository meetingApprovalRepository;
        private readonly IMapper mapper;

        public MeetingAppointServices(IMeetingAppointRepository meetingAppointRepository,IMeetingRoomRepository meetingRoomRepository,IMeetingApprovalRepository meetingApprovalRepository,IMapper mapper)
        {
            this.meetingAppointRepository = meetingAppointRepository;
            this.meetingRoomRepository = meetingRoomRepository;
            this.meetingApprovalRepository = meetingApprovalRepository;
            this.mapper = mapper;
        }

        public async Task<ApiResult> AddAppionMent(AddMeetingReservationDTO meetingReservationDTO)
        {
            try
            {
                var list=await meetingAppointRepository.GetAsync(x => x.MeetingName == meetingReservationDTO.MeetingName);
                if (list != null)
                {
                    return ApiResult.Error(ApiResultCode.Error, "会议已存在");
                }
                using (var tran = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
                {
                    var meetingReservation=mapper.Map<MeetingReservation>(meetingReservationDTO);
                    await meetingAppointRepository.AddAsync(meetingReservation);
                    await meetingApprovalRepository.AddAsync(new MeetingApprove
                    {
                        MeetingReservationId = meetingReservation.MeetingReservationId,
                        RoomName = (await meetingRoomRepository.GetAsync(x => x.Id == meetingReservation.MeetingRoomID)).RoomName,
                        MeetingTheme = meetingReservation.MeetingName,
                        Compere = meetingReservation.Compere,
                        MeetingTime = meetingReservation.MeetingTime,
                        BeginTime = meetingReservation.BeginTime,
                        EndTime = meetingReservation.EndTime,
                        Attendance = 10,
                        SubmissionTime = DateTime.Now,
                        MeetingContent = meetingReservation.MeetingContent,
                        ApprovalNotes = "",
                        Status = ApprovalState.待审批,
                        Result = ApprovalResult.待审批,
                    }
                    );
                    tran.Complete();
                }
                return ApiResult.Success(ApiResultCode.Success);
            }
            catch (Exception)
            {

                throw;
            }
        }

        public async Task<ApiResult<ApiPaging<MeetAppointDTO>>> MeetAppiontList(string? MeetingName, [DefaultValue(1)] int pageIndex, [DefaultValue(10)] int pageSize)
        {
            try
            {
                var meetlist = meetingAppointRepository.GetAll();
                if (!string.IsNullOrEmpty(MeetingName))
                {
                    meetlist = meetlist.Where(x => x.MeetingName.Contains(MeetingName));
                }
                var newlist=mapper.Map<IList<MeetAppointDTO>>(meetlist);
                foreach (var item in newlist)
                {
                    item.RoomName = (await meetingRoomRepository.GetAsync(x=>x.Id == item.MeetingRoomID)).RoomName;
                    item.Location=(await meetingRoomRepository.GetAsync(x=>x.Id==item.MeetingRoomID)).Location;
                    item.Administrator=(await meetingRoomRepository.GetAsync(x=>x.Id==item.MeetingRoomID)).Administrator;
                    item.PhoneNumber=(await meetingRoomRepository.GetAsync(x=>x.Id==item.MeetingRoomID)).ContactNumber;
                }

                int totalcount = newlist.Count();
                int pagecount = (int)(Math.Ceiling(totalcount * 1.0 / pageSize));
                newlist = newlist.OrderBy(x => x.MeetingReservationId).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();

                ApiPaging<MeetAppointDTO> paging = new ApiPaging<MeetAppointDTO>(totalcount, pagecount, newlist);
                return ApiResult<ApiPaging<MeetAppointDTO>>.Success(ApiResultCode.Success, paging);
            }
            catch (Exception)
            {

                throw;
            }
        }

        public async Task<ApiResult> UpdateAppionMent(MeetingReservation meetingReservation)
        {
            try
            {
                var list =await meetingAppointRepository.GetAsync(x => x.MeetingReservationId != meetingReservation.MeetingReservationId&&x.MeetingName==meetingReservation.MeetingName);
                if (list != null)
                {
                    return ApiResult.Error(ApiResultCode.Error, "会议名称已存在");
                }
                using (var tran = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
                {
                    await meetingAppointRepository.UpdateAsync(meetingReservation);
                    var approvalinfo=await meetingApprovalRepository.GetAsync(x=>x.MeetingReservationId==meetingReservation.MeetingReservationId);
                    if (approvalinfo == null)
                    {
                        return  ApiResult.Error(ApiResultCode.Error, "审批信息不存在");
                    }
                    approvalinfo.RoomName = (await meetingRoomRepository.GetAsync(x => x.Id == meetingReservation.MeetingRoomID)).RoomName;
                    approvalinfo.MeetingTheme= meetingReservation.MeetingName;
                    approvalinfo.Compere = meetingReservation.Compere;
                    approvalinfo.MeetingTime= meetingReservation.MeetingTime;
                    approvalinfo.BeginTime = meetingReservation.BeginTime;
                    approvalinfo.EndTime = meetingReservation.EndTime;
                    approvalinfo.Attendance = 10;
                    approvalinfo.SubmissionTime = DateTime.Now;
                    approvalinfo.MeetingContent = meetingReservation.MeetingContent;
                    approvalinfo.ApprovalNotes = "";
                    approvalinfo.Status = ApprovalState.待审批;
                    approvalinfo.Result = ApprovalResult.待审批;
                    await meetingApprovalRepository.UpdateAsync(approvalinfo);
                    tran.Complete();
                }
                return ApiResult.Success(ApiResultCode.Success);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
